<?php

namespace addons\yiliao\controller\api;

use app\admin\model\yiliao\Coupon as CouponModel;
use app\admin\model\yiliao\Usercoupon;
use fast\Http;

class Coupon extends Base
{
    public function _initialize()
    {
        parent::_initialize();
    }

    public function coupondetail()
    {
        $id = $this->request->param('id');
        $isxcx = $this->request->param('isxcx');
        $row = Usercoupon::where(['id'=>$id])->find();
		if($isxcx==1){
			//生成二维码
			$path = 'uploads/yiliao/'.md5($row["id"].$row['createtime']).'.jpg';
			if (!file_exists($path)) {
				$access = $this->getaccess();
				$api = 'https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token='.$access['access_token'];
				$can = [];
				$can['page']='pages/index/index';
				$can['scene']="couponid=".$row['id'];
				$can['check_path']=false;
				$can['auto_color']=false;
				$can['width']=430;
				//$can['env_version']='develop';
				$res = Http::post($api, json_encode($can));
				if (!is_dir('./uploads/yiliao')) {
					mkdir(iconv("UTF-8", "GBK", './uploads/yiliao'), 0777, true);
				}
				$file = fopen($path, "w");//创建件准备写入
				fwrite($file, $res);//写入
				fclose($file);//关闭
			}
			$row['ewm']=$this->apiurl.'/'.$path;
		} else {
			$info = get_addon_info('qrcode');
            if (!$info|| !$info['state']) {
                $this->error('请确保二维码生成插件已安装并启用');
            } else {
				$url = $this->apiurl.'/#/pages/user/mycouponuse/hexiao?id='.$id;
				$row['ewm']=$this->apiurl.'/qrcode/build?text='.urlencode($url);
			}
		}
        
        $this->success('', $row);
    }

    /**
     * 获得可领取优惠券
     */
    public function getCoupon()
    {
        $end = date("Y-m-d");
        $rows = CouponModel::where(['status'=>'normal','end'=>['gt',$end]])->order('id desc')->select();
        $this->success('', $rows);
    }

    /**
     * 领取优惠券
     */
    public function lingqu()
    {
        $id = $this->request->param('id');
        if ($this->user && $this->user['userid'] > 0) {
            $date=date("Y-m-d");
            $data = CouponModel::where(['id'=>$id,'status'=>'normal'])->find();
            if ($data['end'] > $date) {
                $num = Usercoupon::where(['userid'=>$this->user['userid'],'couponid'=>$id])->count();
                if ($data['xianling'] > $num) {
                    $d=[];
                    $d['createtime']=time();
                    $d['updatetime']=time();
                    $d['userid']=$this->user['userid'];
                    $d['couponid']=$id;
                    $d['type']=$data['type'];
                    $d['name']=$data['name'];
                    $d['price']=$data['price'];
                    $d['start']=$data['start'];
                    $d['end']=$data['end'];
                    $d['content']=$data['content'];
                    $res = Usercoupon::insert($d);
                    if ($res) {
                        $this->success('领取成功');
                    } else {
                        $this->error('领取失败');
                    }
                } else {
                    $this->error('每人限领'.$data['xianling'].'个');
                }
            } else {
                $this->error('优惠券已过期');
            }
        } else {
            $this->error('nologin');
        }
    }
    
    /**
     * 我的优惠券
     */
    public function mycoupon()
    {
        $status = $this->request->param('status');
        $date=date("Y-m-d");
        if ($status == 1) {
            $rows = Usercoupon::where(['userid'=>$this->user['userid'],'status'=>'normal'])->select();
        } elseif ($status == 2) {
            $rows = Usercoupon::where(['userid'=>$this->user['userid'], 'status'=>'hidden', 'end'=>['lt',$date]])->select();
        } else {
            $rows = Usercoupon::where(['userid'=>$this->user['userid'],'status'=>'hidden','end'=>['gt',$date]])->select();
        }
    
        $this->success('', $rows);
    }
    
    public function mycoupontj()
    {
        $num1=0;
        $num2=0;
        $num3=0;
        $date=date("Y-m-d");
        $num1 = Usercoupon::where(['userid'=>$this->user['userid'],'status'=>'hidden','end'=>['gt',$date]])->count();
        $num2 = Usercoupon::where(['userid'=>$this->user['userid'],'status'=>'normal'])->count();
        $num3 = Usercoupon::where(['userid'=>$this->user['userid'],'status'=>'hidden','end'=>['lt',$date]])->count();
        $this->success('', ['num1'=>$num1,'num2'=>$num2,'num3'=>$num3]);
    }
}
